package rs.networx.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.FriendRequestDao;
import rs.networx.dao.PersonDao;
import rs.networx.entity.Person;
import rs.networx.util.ErrorMessages;

public class SearchAgainFromRegisteredUserHomePageServlet extends HttpServlet {

	private static final long serialVersionUID = 1814894471114721584L;

	@EJB
	private PersonDao personDao;
	
	@EJB
	private FriendRequestDao friendRequestDao;
	
	private String username;
	private String password;
	private Person loggedUser;
	
	private String searchData;
	
	private List<Person> searchResults;
	private List<Person> resultPersonsIAmNotFriendsWith;
	private List<Person> resultPersonsIAmFriendsWith;
	private List<Person> resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		System.out.println("Still LOADING (SearchAgainServlet)");
		
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());
		
		HttpSession session = request.getSession();
		loggedUser = (Person)session.getAttribute("logovaniUser");

		try {
			searchData = (String)session.getAttribute("searchDataForSearchAgain");
			System.out.println("searchData: " + searchData);
			
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
			System.out.println(username + "  " + password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		
		if(loggedUser == null){
			request.getRequestDispatcher("Welcome.jsp").forward(request, response);
			return;
		}else if(loggedUser != null){
			if(searchData != null && searchData != ""){
				searchResults = personDao.search(searchData);
				if(searchResults == null || searchResults.size() == 0){
					//session.setAttribute("searchStatus", ErrorMessages.NO_SEARCH_RESULTS);
					request.setAttribute("searchStatus", ErrorMessages.NO_SEARCH_RESULTS);
				}else if(searchResults != null && searchResults.size() > 0){
					//session.setAttribute("searchStatus", ErrorMessages.THERE_IS_SEARCH_RESULTS);
					request.setAttribute("searchStatus", ErrorMessages.THERE_IS_SEARCH_RESULTS);
					
					resultPersonsIAmNotFriendsWith = new ArrayList<Person>();
					resultPersonsIAmFriendsWith = new ArrayList<Person>();
					resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo = new ArrayList<Person>();
					
					for(int i = 0; i < searchResults.size(); i++){
						if(searchResults.get(i) instanceof Person){
							Person person = (Person)searchResults.get(i);
							if(loggedUser.getId().equals(person.getId())){
								continue;
							}else{
								if(personDao.areFriends(loggedUser.getId(), person.getId())){
									resultPersonsIAmFriendsWith.add(person);
								}else if(friendRequestDao.checkIfISentFriendRequest(loggedUser, person)){
									resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo.add(person);
								}else{
									resultPersonsIAmNotFriendsWith.add(person);
								}
							}														
						}
					}
					/*session.setAttribute("resultPersonsIAmFriendsWith", resultPersonsIAmFriendsWith);
					session.setAttribute("resultPersonsIAmNotFriendsWith", resultPersonsIAmNotFriendsWith);
					session.setAttribute("resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo", 
							resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo);*/
					
					request.setAttribute("resultPersonsIAmFriendsWith", resultPersonsIAmFriendsWith);
					request.setAttribute("resultPersonsIAmNotFriendsWith", resultPersonsIAmNotFriendsWith);
					request.setAttribute("resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo", 
							resultLicniProfiliKojimaSamPoslaoZahtevZaPrijateljstvo);
				}
			}
			request.getRequestDispatcher("SearchForDude.jsp").forward(request, response);
			return;
		}
	}
		public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}
}
